package com.sam.scanner;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sam.scanner.activity.OutScanerMainActivity;
import com.sam.scanner.activity.ScanerMainActivity;
import com.sam.scanner.activity.SearchScanerActivity;
import com.sam.scanner.bean.LoginBean;
import com.sam.scanner.utils.HTTPCallBack;
import com.sam.scanner.utils.Utils;
import com.sam.scanner.utils.SharedPreferencesUtil;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferencesUtil.init(getApplicationContext());
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA},1);
    }

    String token = "";
    String name = "";
    String phoneNum ="";
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_login){
            Map<String,String> parmas = new HashMap<>();
            parmas.put("telNo","13594516029");
            parmas.put("password","123456");
            Utils.getInstance().post("orderUser/login", new Gson().toJson(parmas),"", new HTTPCallBack() {
                @Override
                public void Succ(String res) {
                    Gson gson = new Gson();
                    LoginBean loginBean = gson.fromJson(res, LoginBean.class);
                    if (loginBean!=null) {
                        token = loginBean.getData().getSessionId();
                        SharedPreferencesUtil.getInstance().putSP("token",token);
                        name = loginBean.getData().getUserName();
                        phoneNum = loginBean.getData().getUserPhone();
                        Log.i("SamTam",token);
                        view.post(()->{
                            ((TextView)findViewById(R.id.tv_loging_data)).setText(loginBean.getData().toString());
                            findViewById(R.id.btn_ex).setVisibility(View.VISIBLE);
                            findViewById(R.id.btn_all).setVisibility(View.VISIBLE);
                            findViewById(R.id.btn_scaner).setVisibility(View.VISIBLE);
                            findViewById(R.id.btn_out_package).setVisibility(View.VISIBLE);
                            findViewById(R.id.btn_search).setVisibility(View.VISIBLE);
                        });


                    }
                }

                @Override
                public void Fail(String errorStr) {
                    view.post(()-> ((TextView)findViewById(R.id.tv_loging_data)).setText(errorStr));
                }
            });
        }else {
            if (token.equals("")) {
                return;
            }
            Intent intent = new Intent(getApplicationContext(),ScanerMainActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("exName",name);
            bundle.putString("exPhoneNum",phoneNum);
            bundle.putString("address","13381800564");
            switch (view.getId()){
                case R.id.btn_ex:
                    bundle.putInt("scanerTag",1);
                    break;
                case R.id.btn_all:
                    bundle.putInt("scanerTag",3);
                    break;
                case R.id.btn_scaner:
                    bundle.putInt("scanerTag",2);
                    break;
                case R.id.btn_out_package:
                    intent = new Intent(getApplicationContext(), OutScanerMainActivity.class);
                    break;
                case R.id.btn_search:
                    intent = new Intent(getApplicationContext(), SearchScanerActivity.class);
                    break;
            }
            intent.putExtras(bundle);
            startActivity(intent);
        }

    }
}
